<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="region"/>
    <#include "../common/header.ftl">
    <link rel="stylesheet" href="/js/plugins/treeView/bootstrap-treeview.min.css" type="text/css"/>
    <script type="text/javascript" src="/js/plugins/treeView/bootstrap-treeview.min.js"></script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">旅游区域管理</h1>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-12">
                    <a role="button" class="btn btn-success" id="add">
                        <span class="glyphicon glyphicon-plus"></span> 添加区域
                    </a>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-4">
                    <div id="treeview"></div>
                </div>
                <div class="col-lg-8">
                    <table class="table table-striped table-hover" style="display: none">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>区域</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>xx</td>
                            <td>xx</td>
                            <td>
                                <a href="javascript:" class="btn btn-info btn-xs inputBtn">
                                    <span class="glyphicon glyphicon-pencil"></span> 修改
                                </a>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form id="editForm" class="form-horizontal" method="post" action="/region/saveOrUpdate.do">
                    <input type="hidden" name="id" value=""/>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">上级区域</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parent.name" readonly/>
                            <input type="hidden" class="form-control" name="parent.id"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">区域名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入区域名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">设为推荐</label>
                        <div class="col-sm-6">
                            <select name="state" class="form-control">
                                <option value="0">普通</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:" class="btn btn-success submitBtn" aria-hidden="true">保存</a>
                <a href="javascript:" class="btn btn-info" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        var tr = $("table tbody tr").clone();
        $.get("/region/queryTreeView.do?treeView=true", function (data) {
            $("#treeview").treeview({
                color: "#be1ae3",
                showTags: true,
                lazyLoad: function (node) {
                    $.get("/region/queryTreeView.do", {parentId: node.id, treeView: true}, function (data) {
                        $("#treeview").treeview("addNode", [data, node])
                    })

                },
                onNodeSelected: function (ev, node) {
                    $("table tbody tr").html("");
                    $.get("/region/queryTreeView.do", {parentId: node.id}, function (data) {
                        $.each(data, function (index, ele) {
                            var row = tr.clone();
                            row.children()[0].innerHTML = index + 1;
                            row.children()[1].innerHTML = ele.name;
                            row.find("a").attr("data-json",ele.json);
                            row.appendTo("table tbody")
                        })
                        $("table").show();

                        $(".inputBtn").click(function () {
                            var json = $(this).data("json");
                            $("#inputModal").modal("show");
                            $("[name=id]").val(json.id)
                            $("[name=name]").val(json.name);
                            $("[name='parent.name']").val(json.parentName);
                            $("[name=state]").val(json.state);
                        })

                    })

                },
                data: [{
                    text: "全部区域",
                    nodes: data
                }]
            });

        })

        $("#add").click(function () {
            var node = $("#treeview").treeview("getSelected")[0];
            $("#inputModal").modal("show");
            $("#editForm")[0].reset();
            if (!node) {
                $("[name='parent.name']").val("全部区域");
                return;
            }
            $("[name='parent.name']").val(node.text);
            $("[name='parent.id']").val(node.id);
        })


        $(".submitBtn").click(function () {
            console.log(1);
           $("#editForm").ajaxSubmit(function (data) {
               if(data.success){
                   $.messager.alert("温馨提示","操作成功!");
                   setTimeout(function () {
                       location.reload();
                   },500)
               }
           })
        })


    })
</script>
</body>
</html>